﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
namespace XCI.Core
{
    /// <summary>
    /// 分页参数
    /// </summary>
    public class PageParam
    {
        /// <summary>
        /// 构造默认分页条件
        /// </summary>
        public PageParam()
        {
        }

        /// <summary>
        /// 按指定条件构造分页条件
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        public PageParam(int pageIndex, int pageSize)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
        }

        /// <summary>
        /// 按指定条件构造分页条件
        /// </summary>
        /// <param name="pageIndex">页码索引,从1开始</param>
        /// <param name="pageSize">每页记录数</param>
        /// <param name="orderName">排序字段名称</param>
        /// <param name="dirDir">排序方式(asc/desc)</param>
        public PageParam(int pageIndex, int pageSize, string orderName, string dirDir)
        {
            PageIndex = pageIndex;
            PageSize = pageSize;
            SortName = orderName;
            SortDir = dirDir;
        }

        /// <summary>
        /// 页码索引,从1开始
        /// </summary>
        public int? PageIndex { get; set; }

        /// <summary>
        /// 每页记录数
        /// </summary>
        public int? PageSize { get; set; }

        /// <summary>
        /// 排序字段名称
        /// </summary>
        public string SortName { get; set; }

        /// <summary>
        /// 排序方式(asc/desc)
        /// </summary>
        public string SortDir { get; set; }

        /// <summary>
        /// 指定默认的排序信息,如果已经指定了,则不设置.
        /// </summary>
        /// <param name="orderName">排序字段名称</param>
        /// <param name="orderDir">排序方式(asc/desc)</param>
        public void SetDefaultOrder(string orderName, string orderDir)
        {
            if (string.IsNullOrEmpty(SortName))
            {
                SortName = orderName;
            }
            if (string.IsNullOrEmpty(SortDir))
            {
                SortDir = orderDir;
            }
        }

        /// <summary>
        /// 指定默认的排序信息,如果已经指定了,则不设置.
        /// </summary>
        /// <param name="orderName">排序字段名称</param>
        /// <param name="isDesc">是否倒序</param>
        public void SetDefaultOrder(string orderName, bool isDesc = true)
        {
            if (string.IsNullOrEmpty(SortName))
            {
                SortName = orderName;
            }
            if (string.IsNullOrEmpty(SortDir))
            {
                SortDir = isDesc ? "desc" : "asc";
            }
        }

        public void Clear()
        {
            PageIndex = null;
            PageSize  = null;
            SortName = null;
            SortDir  = null;
        }
    }
}